package cn.tedu.micro.service.cart.controller;

import cn.tedu.micro.service.cart.service.CartService;
import cn.tedu.micro.service.common.JsonResult;
import cn.tedu.micro.service.pojo.param.CartAddParam;
import cn.tedu.micro.service.pojo.po.CartPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/cart")
@RefreshScope
public class CartController {
    @Autowired
    private CartService cartService;

    @Value("${server.username}")
    private String username;

    @PostMapping("/add")
    public JsonResult addCart(CartAddParam cartAddParam){
        Boolean result = cartService.addCart(cartAddParam);
        if(result==false)
            return JsonResult.fail("新增失败");
        return JsonResult.ok("添加成功");
    }

    @GetMapping("/select")
    public JsonResult selectCart(){
       return JsonResult.ok(username);
    }

    @PostMapping("/delete")
    public  JsonResult deleteCart(@RequestParam("productCode") String productCode,
                                  @RequestParam("userId") String userId){
        Boolean result = cartService.deleteCart(productCode, userId);
        if(result==false)
            return JsonResult.fail("删除失败");
        return JsonResult.ok("删除成功");
    }
}
